Perl scares me ...

Discussion in 'Perl Misc' started by Richard Gration, Apr 27, 2004.

  1. While trying to work out the syntax for building a dispatch table of
    coderefs from strings in a database I had occasion to construct the
    following:

    #!/usr/bin/perl

    use strict;
    use warnings;

    my $one = "mysub";

    sub mysub {
    print shift,"\n";
    }

    #1
    {
    no strict 'refs';
    $one->('scalar');
    }

    #2
    {
    'mysub'->('string');
    }

    #3
    {
    my $subref = \&$one;
    $subref->('subref');
    }

    This produces the output:

    scalar
    string
    subref

    If there was any further proof needed that Perl updates are received in a
    back alley somewhere from some dude with red eyes who smells of sulphur
    .... I mean, honestly !!! ;-)

    And then there's my slight amazement that #1 needs "no strict 'refs'" and
    #2 doesn't ...

    Rich
    Richard Gration, Apr 27, 2004
    #1
    1. Advertising

  2. Richard Gration

    Matija Papec Guest

    X-Ftn-To: Richard Gration

    "Richard Gration" <> wrote:
    >If there was any further proof needed that Perl updates are received in a
    >back alley somewhere from some dude with red eyes who smells of sulphur
    >... I mean, honestly !!! ;-)


    Err, and what was your expected output?

    >And then there's my slight amazement that #1 needs "no strict 'refs'" and
    >#2 doesn't ...


    References in perl are very well documented, you may want to check "perldoc
    perlref"



    --
    Matija
    Matija Papec, Apr 27, 2004
    #2
    1. Advertising

  3. "Richard Gration" <> writes:

    > my $one = "mysub";


    > #1
    > $one->('scalar');


    > #2
    > 'mysub'->('string');


    > And then there's my slight amazement that #1 needs "no strict 'refs'" and
    > #2 doesn't ...


    Yeah, it's a known problem (er.. bug) that strict does not pick-up on
    stuff that gets optomised away during compliation. Indeed there seems
    to have been a spate of threads about this lately.

    --
    \\ ( )
    . _\\__[oo
    .__/ \\ /\@
    . l___\\
    # ll l\\
    ###LL LL\\
    Brian McCauley, Apr 27, 2004
    #3
  4. Richard Gration

    pkent Guest

    In article <c6lqjn$kgc$2surf.net>,
    "Richard Gration" <> wrote:

    > While trying to work out the syntax for building a dispatch table of
    > coderefs from strings in a database I had occasion to construct the
    > following:

    ....
    <snip example code>


    Obviously I have no idea what you were trying to do, or what your
    restrictions were, but... wasn't there a better way? :) My dispatch
    tables always look something like:

    %foo = (
    bar => \&bar,
    baz => \&baz,
    # etc...
    );

    # insert more code here

    sub bar {
    my ($thing, $whatever, $boo) = @_;
    # some code here
    }


    and as luck would have it I'm working on a database-based app right now
    at work, too.

    P

    --
    pkent 77 at yahoo dot, er... what's the last bit, oh yes, com
    Remove the tea to reply
    pkent, Apr 27, 2004
    #4
  5. In article <>,
    "pkent" <> wrote:


    > In article <c6lqjn$kgc$2surf.net>,
    > "Richard Gration" <> wrote:
    >
    > Obviously I have no idea what you were trying to do, or what your
    > restrictions were, but... wasn't there a better way? :) My dispatch
    > tables always look something like:
    >
    > %foo = (
    > bar => \&bar,
    > baz => \&baz,
    > # etc...
    > );
    > # insert more code here
    > sub bar {
    > my ($thing, $whatever, $boo) = @_;
    > # some code here
    > }


    Well, mine used to too, but then I was faced with coming up with a way to
    make the same code operate the dispatch table for different websites,
    each with their own database, hence moving the config into the db and
    building the dispatch table from there, rather than having in the code
    blocks like

    if (host is websiteA) {
    dispatch_table is ...
    } elsif (host is websiteB) {
    dispatch_table is ...
    } ...

    which I'm trying to get away from.

    > and as luck would have it I'm working on a database-based app right now
    > at work, too.


    Hmm ... luck would be sitting on a Caribbean beach with a rum based
    cocktail in one hand and a babe in the other ;-) But ISWYM :)
    Richard Gration, Apr 28, 2004
    #5
  6. Richard Gration

    Joe Smith Guest

    Richard Gration wrote:
    > Well, mine used to too, but then I was faced with coming up with a way to
    > make the same code operate the dispatch table for different websites,
    > each with their own database, hence moving the config into the db and
    > building the dispatch table from there, rather than having in the code
    > blocks like
    >
    > if (host is websiteA) {
    > dispatch_table is ...
    > } elsif (host is websiteB) {
    > dispatch_table is ...
    > } ...
    >
    > which I'm trying to get away from.


    $_ = 'websiteA'
    $dispatch_table = $table{$_};

    or

    $result = $table{$website}->function($args);
    Joe Smith, Apr 28, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,765
  2. Replies:
    1
    Views:
    353
    Steven T. Hatton
    Apr 1, 2004
  3. PerlFAQ Server

    FAQ 1.4 What are Perl 4, Perl 5, or Perl 6?

    PerlFAQ Server, Jan 23, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    285
    PerlFAQ Server
    Jan 23, 2011
  4. PerlFAQ Server
    Replies:
    0
    Views:
    652
    PerlFAQ Server
    Feb 3, 2011
  5. PerlFAQ Server

    FAQ 1.4 What are Perl 4, Perl 5, or Perl 6?

    PerlFAQ Server, Feb 27, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    276
    PerlFAQ Server
    Feb 27, 2011
Loading...

Share This Page