Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^2: Could I get some feedback on my code please?

by PockMonk (Beadle)
on Jan 13, 2007 at 12:13 UTC ( [id://594529]=note: print w/replies, xml ) Need Help??


in reply to Re: Could I get some feedback on my code please?
in thread Could I get some feedback on my code please?

Wow, lots of stuff to look at there, thanks. I have a few questions though. you say:

"Do not use the special &-syntax to call subroutines unless you understand and require its special semantics. "

My copy of the Programming Perl book makes no mention of this, should I never use the "&" in calling subroutines??? This is the first I've heard of this! Can you eplain please?

"You are using the global file handle TOKENFILE. This habit could cause you troble in future. You could local(*TOKENFILE) to make your changes to the global handle temporary but better to simply not use a global handle. "

Can you point me to a page on this or explain this further please?

  • Comment on Re^2: Could I get some feedback on my code please?

Replies are listed 'Best First'.
Re^3: Could I get some feedback on my code please?
by f00li5h (Chaplain) on Jan 13, 2007 at 12:52 UTC

    Calling a sub with &sub will do many things, but most interstingly, it will not set @_ inside the sub, so you will inherit the @_ from the outer context, the & also means that prototypes are ignored for that call.

    If you're not sure what those are, it's safer to not tamper with them, and just call subs with sub().

    Somewhere it says in Learning Perl, "you don't tell someone to 'do wash the dishes' when you can just say 'wash the dishes'" this is a terrible paraphrase, but i think i left the book at work ...

    @_=qw; ask f00li5h to appear and remain for a moment of pretend better than a lifetime;;s;;@_[map hex,split'',B204316D8C2A4516DE];;y/05/os/&print;
Re^3: Could I get some feedback on my code please?
by Joost (Canon) on Jan 13, 2007 at 12:56 UTC
    Look at perlsub: &subname(); will disable prototype-checking for that call (now prototypes aren't used much in perl, for good reasons, but if they're there, you'll probably don't want to skip the checks).

    Also, if you forget the parenthesis and call the sub as &subname; you will forward your current @_ array to that call. In other words, you will pass on your current arguments - not call it without arguments.

    Instead of doing that, you should probably always write subname( args ) (i.e. with parenthesis and without the ampersand). It's the only way to call a subroutine that doesn't have any hidden snags. Generally, you'll only use the & sygil if you want to take references to named subroutines or for goto &subroutine which is only useful in very specific cases.

Re^3: Could I get some feedback on my code please?
by SFLEX (Chaplain) on Jan 13, 2007 at 12:56 UTC
    using "&" is not needed in most parts of your code, but is still used for some things.
    From what I have read, tells to save the use of "&" for calling subroutines to equale other stuff like
    $SIG{__DIE__} = \&fatal_error;
    and other uses.
    Perl 5 still supportes "&" but the preferd format is like "a_sub();" the "&" is considered as clutter.

    A Link to Chapter 9 of Perl Best Practices page 176 explains this and I suggest to reading the hole Chapter and/or buy the book.

    Good Luck

Re^3: Could I get some feedback on my code please?
by Errto (Vicar) on Jan 14, 2007 at 02:50 UTC
    About the file handle thing: in Perl 5 a file handle can be stored as a reference in a scalar variable. As of Perl 5.6 (I think), the builtin open function can initialize this variable for you:
    open my $tokenfile, '<', $filename or die "Couldn't open file: $!";
    Now $tokenfile is your filehandle and you don't have to worry about it being global or whatever. Have a read through perlopentut - it talks more about this.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (None)
    As of 2024-04-19 00:04 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found