use File::Spec;
my $package = 'Foo::Bar::Baz';
require File::Spec->catfile(split /::/, $package);


more generally, this works for me:

$tranformed = map {local $_=$_; s,::,/,g; $_} $package

Greg

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, May 04, 2004 4:14 PM
To: [EMAIL PROTECTED]
Subject: s/// w/o intermediate variables?






Here's an example of a recurrent annoyance:

  my $package = 'Foo::Bar::Baz';
  (my $package_filename = $package) =~ s,::,/,g;
  require $package_filename;
  $package->foobar();

One of my many neurotic little peeves is that, unless code readability
absolutely demands it, I hate defining variables that will be used
only once, such as $package_filename above.  I would like to be able
to do something like

  my $package = 'Foo::Bar::Baz';
  require TRANSFORM[ s,::,/,g, "$package.pm" ];
  $package->foobar();

where TRANSFORM[] stands for an expression in which an "s-expression"
(i.e. one using s///) is applied to a string, and the resulting string
is returned as the result.

Of course, I could define a helper sub to do this:

  sub transform {
    my $s_expression = shift;
    local $_ = shift;
    eval $s_expression;
    die $@ if $@;
    $_
  }

But is there a way to achieve this result without defining such a
helper sub?

kynn

P.S. I couldn't come up with a sufficiently general transform sub such
that its first argument is a qr-quoted regexp.  Is there a way to do
this that approaches the level of generality of the eval kluge above?


------------------------------------------------------------------------------
This message is intended only for the personal and confidential use of the
designated recipient(s) named above.  If you are not the intended recipient of
this message you are hereby notified that any review, dissemination,
distribution or copying of this message is strictly prohibited.  This
communication is for information purposes only and should not be regarded as
an offer to sell or as a solicitation of an offer to buy any financial
product, an official confirmation of any transaction, or as an official
statement of Lehman Brothers.  Email transmission cannot be guaranteed to be
secure or error-free.  Therefore, we do not represent that this information is
complete or accurate and it should not be relied upon as such.  All
information is subject to change without notice.

Reply via email to