Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Use of Typeglob

by ikegami (Patriarch)
on Aug 09, 2005 at 14:00 UTC ( [id://482216]=note: print w/replies, xml ) Need Help??


in reply to Use of Typeglob

Example one:

open(FILE, '>', 'temp.txt') or die("Can't create output file: $!"); greet(*FILE); sub greet { my ($fh) = @_; print $fh "Hello World\n"; }

Example two:

sub foo { print("Hello World\n"); } *bar = \&foo; bar();

Most scripts don't use typeglobs. They just reap the benefits of very special modules which use them. For example, Exporter and constant might use typeglobs to import the symbols into the caller's namespace.

The top example can be rewritten without typeglobs. It's safer without them.

open(my $file, '>', 'temp.txt') or die("Can't create output file: $!"); greet($file); sub greet { my ($fh) = @_; print $fh "Hello World\n"; }

Note how greet didn't change? Where file handles are concerned, typeglobs, ref to typeglobs and IO::Handle objects are almost always interchangable.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2024-04-25 02:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found