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

perl 5.005_03 woes

by the_slycer (Chaplain)
on Aug 07, 2003 at 17:57 UTC ( [id://281980]=perlquestion: print w/replies, xml ) Need Help??

the_slycer has asked for the wisdom of the Perl Monks concerning the following question:

Ahhh.. it's nice to be back at the monestary, it's been too long since I've visited (must.. stop.. coding.. delphi).
I have a piece of code like this
my %dispatch = ( 'sub_one' => \&sub_one, 'sub_two' => \&sub_two ); $dispatch{'sub_one'}('arg'); $dispatch{'sub_two'}('arg'); sub sub_one { $a = shift; print "Got to sub one: $a\n"; } sub sub_two { $a = shift; print "got to sub two $a\n"; }
It simply saves me having to write out a whack of if statements. (if this event call this subroutine), as well as allows me to quickly add handlers for various events.

I like it, but, apparently perl 5.005_03 doesn't. I don't have control over where this script that I've written will run, and would like it to be as portable as possible.

My question is this:
Is there a way to replicate this behaviour for those that run perl 5.005_03? I'm hoping for a solution other than if statements, but if that's where I need to go, I will :(

Replies are listed 'Best First'.
Re: perl 5.005_03 woes
by liz (Monsignor) on Aug 07, 2003 at 18:22 UTC
    $dispatch{'sub_one'}->('arg'); $dispatch{'sub_two'}->('arg');
    works for me in 5.005_03.

    Liz

Re: perl 5.005_03 woes
by Limbic~Region (Chancellor) on Aug 07, 2003 at 18:27 UTC
    the_slycer,
    In addition to the proper de-referencing that liz illustrates, you are using $a as an unscoped global. You should avoid using $a and $b as variable names as they are used by Perl for sorting. See perldoc -f sort. It should then be properly scoped with my or *gasp* our if you really need it to be global.

    Welcome back - L~R

      If my memory serves me correctly, couple of things to be aware of:
      • 5.005.03 doesn't do "our"; use use vars instead
      • 5.005.03 doesn't do like use warnings; use /my/perl -w instead

      -------------------------------------
      Nothing is too wonderful to be true
      -- Michael Faraday

      Yah, I knew/know the $a/$b bit actually (should have thought before posting), I never use either in production code. Was just making it simple for someone that was testing it for me. I always use warnings; use strict; (there was another "forgotten" surprise from that btw :) ).
        the_slycer,
        Actually I assumed you did have:
        #!/usr/bin/perl -w use strict;
        In the small snippet you showed on 5.6.1 you would not have encountered any problems. That is why I didn't specifically mention them. This is because the strict pragma ignores $a and $b since Perl uses them to allow you to build a custom sort routine.

        Cheers - L~R

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-23 13:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found