Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

asterisk use

by smist (Acolyte)
on Jan 10, 2007 at 19:19 UTC ( #593984=perlquestion: print w/replies, xml ) Need Help??
smist has asked for the wisdom of the Perl Monks concerning the following question:

Hello. I'm using find2perl to generate some code and the code below that was generated doesn't make sense to me. What are the asterisks used for? I know I've seen something like this before but I cannot remember where and how it works. Thanks!
use vars qw/*name *dir *prune/; *name = *File::Find::name; *dir = *File::Find::dir; *prune = *File::Find::prune;

Replies are listed 'Best First'.
Re: asterisk use
by davido (Archbishop) on Jan 10, 2007 at 19:26 UTC

    Those are typeglobs, and are being used to make aliases to variables embedded within the File::Find module.

    For example, $name will be an alias to $File::Find::name.

    That way, the package global, $name, which presumably resides in package main (otherwise known as $main::name) will be the same variable as $File::Find::name; a package global in the File::Find module.


    Dave

      To slightly extend davido's explanation with a more concrete example (because that's how I learn anyway):

      This first example uses the full variable name to find all instances of mail.log.* in the current directory and pushes the results on @files.

      use File::Find; find( sub { push @files, $File::Find::name if -f && /mail.log.*/ }, '. +');

      This second example does the same thing only using the glob'd version of the variable name.

      use File::Find; use vars qw/*name/; *name = *File::Find::name; find( sub { push @files, $name if -f && /mail.log.*/ }, '.');
        In the subroutine named "wanted" that find2perl creates there is usage of the "&&" operator. If I understand it correctly, it's evaluating each expression, and if they evaluate to "true" it prints $name. Is the last "&&" necessary? There are no expressions after it; just goes to the print statement.

        sub wanted { my ($dev,$ino,$mode,$nlink,$uid,$gid); (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) && -d _ && print("$name\n"); }
        Thanks guys. I knew I read about this somewhere before. I just couldn't remember what it was. I haven't used globbing yet and I'm pretty new to Perl.

        Your help is appreciated!
Re: asterisk use
by imp (Priest) on Jan 10, 2007 at 19:23 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://593984]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
[choroba]: When working at a bank, we had a colleague who entered mining early enough to have lots of bitcoins. He used to say "I go to work just because I'm too bored at home."
[Corion]: choroba: Aah, no, I do it because of the money :)
[choroba]: And when asked "How are you?" in the morning, his reply was usually "I don't know, haven't checked the bitcoin rates yet."
[Corion]: Heh - if I defined myself through the money I make, I would now only be 80% myself :-D
[choroba]: :-D
LanX emacs is the oldest textadventure ...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (12)
As of 2017-09-21 15:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (249 votes). Check out past polls.

    Notices?