Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^2: Using guards for script execution?

by AnomalousMonk (Archbishop)
on Mar 01, 2017 at 01:53 UTC ( [id://1183229]=note: print w/replies, xml ) Need Help??


in reply to Re: Using guards for script execution?
in thread Using guards for script execution?

package Blah; { sub foo { print "bar!\n"; } } package main; { foo(); }

This code will define  foo() in the  Blah package; it will not be available in the  main package unless exported to it or invoked as a fully qualified subroutine (called without warnings or strictures):

c:\@Work\Perl\monks\R0b0t1>perl -le "package Blah; { sub foo { print \"bar!\n\"; } } package main; { foo(); } " Undefined subroutine &main::foo called at -e line 1. c:\@Work\Perl\monks\R0b0t1>perl -le "package Blah; { sub foo { print \"bar!\n\"; } } package main; { Blah::foo(); } " bar!

On a tangential note, the package syntax

{ package Foo; my $x = ...; ... sub bar { ... } ... }
and from Perl version 5.14 onward
package Foo { my $x = ...; ... sub bar { ... } ... }
will cause the asserted package to be "turned off" at the end of the block, with reversion to the "original" package. E.g., (with full warnings and strictures):
c:\@Work\Perl\monks\R0b0t1>perl -wMstrict -le "print 'perl version: ', $]; ;; in_pkg('A'); ;; package Foo { ::in_pkg('B'); } ;; in_pkg('C'); ;; sub in_pkg { print qq{$_[0]: in package }, scalar caller; } " perl version: 5.014004 A: in package main B: in package Foo C: in package main


Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1183229]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2026-04-19 15:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.