SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    Web Genius
    Join Date
    Nov 2001
    Location
    Canada
    Posts
    708
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Perl subroutines, includes and ugly variable scope!

    ICK!

    I've been coding in a Perl for the last while coming from a PHP background and all I can say is ick! Dealing with Perl variable scope and declarations is no fun at all.

    It gets especially messy! when you start trying to use "packages".
    use mypackage;

    The namespaces get out of control and it seems you have to delcare variables all over the place over and and over.
    Maybe because I'm using strict and warnings?

    my var;

    sub goHome {
    my ($passed) = @_;
    print "$passed";
    }
    sub doSomething {
    &goHome("$var eeek");
    }


    Use of uninitialized value in concatenation (.) or string at

    Perhaps I'm just not use to the convention or something but I'm not enjoying Perl at all. I'm considering rewriting the entire app in Python since I think it's a bit more PHP guy friendly.

    End ICK

  2. #2
    Non-Member
    Join Date
    Aug 2009
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    my var;
    should be
    my $var;

    &goHome("$var eeek");
    should be
    goHome("$var eeek");

  3. #3
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vasyapupkin View Post
    &goHome("$var eeek");
    should be
    goHome("$var eeek");
    I'm quite new to Perl, but my understanding was that it's okay to prefix sub calls with '&', especially if you're not sure whether or not you're using the same name as a built-in sub.
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  4. #4
    Non-Member
    Join Date
    Aug 2009
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can surely rewrite it in any language you want, including Haskell and x86 assembly, but you won't get better results unless you learn that language first

  5. #5
    SitePοint Troll disgracian's Avatar
    Join Date
    Aug 2006
    Location
    Samsara
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It is okay, but it's only necessary under some circumstances. I generally prototype all subroutines in my scripts which negates the need to prefix the sub with '&'. The only circumstances I can think of where you want/need to prefix a sub call with '&' are:
    • You want to ignore the prototype.
    • You call the subroutine via a reference.

    See perlsub and perlref for more details.

    Cheers,
    D.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •