Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Beatnik (493)

  (email not shown publicly)

A 29 year old belgian who likes Mountain Dew, Girl Scout Cookies, Tim Hortons French Vanilla Flavoured Cappucinno, Belgian beer, Belgian chocolate, Belgian women, Magners Cider, chocolate chipped cookies and Perl. Likes snowboarding, snorkling, sailing and silence. Bach can really cheer him up! He still misses his dog.

Project Daddy of Spine [], a mod_perl based CMS.

In his superhero time (8.30 AM to 5.30 PM), he works on world peace.

Journal of Beatnik (493)

Monday May 19, 2003
07:38 AM

Schwartzian Magic

[ #12298 ]
I never thought I'd say this but for some reason the Schwartzian Transformation seemed like the only way to solve a problem I was having...

eval qq!
if ("$params{'sort'}" eq "date")
{ use Time::Local;
  \@messages =
  map { \$_->[0] }
  sort { \$a->[1] <=> \$b->[1] }
  map { my (\$h,\$m,\$d,\$M,\$y) =
  \$_->messagedate =~ \/^(\\d{1,2})\\\:(\\d{1,2})
  [\$_,timelocal(0,\$m,\$h,\$d,\$M-1,\$y) ] }
  \@messages; #Schwartzian Magic
} else
{ \@messages =
  { lc(\$a->$params{'sort'}) cmp
    lc(\$b->$params{'sort'}) } \@messages;
warn $@ if $@;

For some reason, I couldn't sort if from the DB but I needed to sort it properly.

Annika and Els, I'm here for you!

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • My only question is: why are you using eval "" rather than eval {} ?
      ---ict / Spoon
    • Because I need some vars interpolated and some don't. The $params{'sort'} is dynamic.
      • You still don't need the string eval e.g

        if ($params{sort} eq "date")
        { use Time::Local;
          @messages =
          map  { $_->[0] }
          sort { $a->[1] <=> $b->[1] }
          map  { my ($h,$m,$d,$M,$y) =
            $_->messagedate =~ /^(\d{1,2})\:(\d{1,2})
            [$_,timelocal(0,$m,$h,$d,$M-1,$y) ] }
          @messages; #Schwartzian Magic
        } else
        { @messages =
          { lc($a->${\$params{sort}}) cmp


        broquaint out

        • @messages contains a number of objects and I need to call a simple getter. I can't (and shouldn't) access those variables explicitly. So I need the name of that parameter (and yes, I do some checking prior to that eval block). And yes, I am using strict... all of it :P
          • I still don't need the need for strict. The only bits that are interpolated are the string comparison, which doesn't need to be interpolated, and the methods calls, which again, can live outside a string eval. Or am I missing something?

            broquaint out

            • IIRC you can't do method calls based on scalar values within strict. Ofcourse, I still suck at perl ;P
              • You surely can

                shell> perl -Mstrict -

                sub foo { print "i'm in foo\n" }
                my $m = "foo";

                i'm in foo

                This is because we're not violating strict in anyway (not symbolic, not bareword, declared variable).

                With your hash you can do some slippery scalar dereferencing (to keep it all syntactically correct) or just assign the value to a simple scalar e.g

                shell> perl -Mstrict -

                sub foo { print "i'm in foo\n" }

                my %h = ( method => "foo" );
                my $m = $h{method};

                # slippery syntactic shenanigans


                broquaint out