[31521] in Perl-Users-Digest
Perl-Users Digest, Issue: 2780 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Jan 21 09:09:44 2010
Date: Thu, 21 Jan 2010 06:09:08 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Thu, 21 Jan 2010 Volume: 11 Number: 2780
Today's topics:
Re: alternatives for branching <cartercc@gmail.com>
Re: alternatives for branching <OJZGSRPBZVCX@spammotel.com>
Re: alternatives for branching sln@netherlands.com
Re: alternatives for branching <cartercc@gmail.com>
Re: FAQ 7.28 How do I clear a package? <ben@morrow.me.uk>
macros: return or exit <marc.girod@gmail.com>
Re: macros: return or exit <ben@morrow.me.uk>
select the correct answer <abu.4000@gmail.com>
Re: select the correct answer <ace@nosite.com>
Re: select the correct answer <tadmc@seesig.invalid>
Strip control characters in a file <marc.girod@gmail.com>
Re: Strip control characters in a file <uri@StemSystems.com>
Re: Variable set to 0 <sreservoir@gmail.com>
Re: Variable set to 0 <sreservoir@gmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 20 Jan 2010 05:40:14 -0800 (PST)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: alternatives for branching
Message-Id: <93a67b71-3271-47cb-9eb1-17454063891c@l19g2000yqb.googlegroups.com>
On Jan 19, 5:32=A0pm, Ben Morrow <b...@morrow.me.uk> wrote:
> My first response would be 'see Catalyst'. I'm certain it can be made to
> do what you want with your URLs fairly easily, though it would require
> completely rewriting the app (splitting it into multiple parts, so that
> each 'elsif' branch below becomes a method in a controller class).
Ben, there are many web frameworks, and after experimenting with
several I've kinda given up in disgust. You spend a lot of effort
learning the framework (in my case anyway) and then get frustrated by
trying to make it do things you can't persuade it to do. I find it
easier to create my own framework with just the functionalities I
need, and I don't especially want to rewrite the applications.
> The usual alternatives to a whole lot of elsifs are either a dispatch
> table or an object with methods (and now, of course, we have given/when,
> though I'm not yet convinced how much it buys you). It's not clear below
> whether you need those matches to be matches, or whether you can extract
> the interesting bit of $subpage and use it as a key to a hash or a
> method name.
I have read through but not digested both Conways OOPerl and Dominus'
HOP. I have some experience writing OO apps in Java (mostly), C++, and
Lisp, but for some reason have had a lot of trouble applying these
lessons to Perl. I really like HOP in general, and the dispatch table
(chapter 1 or 2) appeals to me. I use hashes almost exclusively to
initialize environmental variables for my scripts that use then, but
these are true variables, not behaviors.
Maybe I'll experiment and learn a new way New for me) of doing things.
> In general, if your conditions are truly arbitrary there is no
> alternative to a great big control structure, so you need to find
> subsets of conditions which are 'the same' and turn them into dispatch
> tables. If your logic is complicated you may find you need a much
> shorter if/elsif construction which simply chooses which table to
> dispatch on and where to get the key from.
Okay. Let me ask this. Is it better to encapsulate your functions in a
sub and call the sub or to call the functions directly in some kind of
way? How do I handle the issue of passing the correct arguments to the
functions? (The parameters are read into global variables because of
how the HTTP form elements are sent to the script.)
CC
------------------------------
Date: Wed, 20 Jan 2010 21:48:16 +0100
From: "Jochen Lehmeier" <OJZGSRPBZVCX@spammotel.com>
Subject: Re: alternatives for branching
Message-Id: <op.u6umqqpnmk9oye@frodo>
On Wed, 20 Jan 2010 14:40:14 +0100, ccc31807 <cartercc@gmail.com> wrote:
> Is it better to encapsulate your functions in a
> sub and call the sub or to call the functions directly in some kind of
> way?
What do you mean by "function"? Do you mean the payload, i.e. the calls
from inside the "if {}" blocks in your example? Best keep giving us
examples of what you mean.
Anyway, whatever you do, I guess it's mostly a question of what looks good
to yourself. Unless you get lots of requests per second, performance will
not differ much between the alternatives. If I understood you correctly,
you're not about to create a great new framework, but just trying to
organise your code a bit more maintainable.
> How do I handle the issue of passing the correct arguments to the
> functions?
As you're in a CGI setting, your arguments are all named (i.e., key-value
pairs)? If so, then passing them as a hash seems appropriate.
For example:
sub function
{
my %args = @_;
print "name is ".$args{name};
}
function(name => "xyz");
(Remember, an array, if interpreted as a hash, works just fine; adjancent
pairs are transformed into key+value).
Or, if that array-as-hash thing bothers, you, feel free to:
sub function
{
my ($args)=@_;
print "name is ".$args->{name};
}
function({name => "xyz"});
Again, as before, there are lots of different ways to handle this. For
example, the CGI library allows you to pass in either named arguments (as
in my first example) or unnamed positional arguments, or even a mixture,
by using the convention that the named arguments begin with a "-" in their
name, and some additional parsing. Check out "perldoc -m CGI" if you're
interested.
------------------------------
Date: Wed, 20 Jan 2010 14:38:01 -0800
From: sln@netherlands.com
Subject: Re: alternatives for branching
Message-Id: <231fl51tqeqqlkavf3a8gduv5mac9p5pg6@4ax.com>
On Tue, 19 Jan 2010 13:49:43 -0800 (PST), ccc31807 <cartercc@gmail.com> wrote:
>In a web application that I maintain, the main logic contains several
>dozen elsif statements nested to several levels. The tests aren't all
>the same, although in the code sample I've posted below they are the
>same. I can maintain this fairly easily but I'd like to redo the logic
>as it seems unwieldy and ugly to me.
>
>This is a database application, and each branch typically calls two
>procedures, the first running an SQL statement and the second a
>display routine.
>
>Is there a Perl alternative to this logic? If so, how could this be
>rewritten in Perl? In general, what are alternatives to a whole bunch
>of elsif statements nested to several levels?
>
>Thanks, CC.
>
Typically you could make a package out of it.
The biggest problem is not the branching, its the organization
of your data. How do you blindly know what to set the data
before even branching?
You need to figure out the data handling before making a package.
Below lays out organized data for that decision.
-sln
use strict;
use warnings;
## Globals
#
my %Packet = (
'sesskey' => '',
'foruser' => '',
'calid' => '',
'event' => '',
'description' => '',
'place' => '',
'eventdate' => '',
'type' => '',
'uniform' => '',
'required' => '',
'display' => '',
'comments' => '',
'newsid' => '',
'username' => '',
'head' => '',
'first' => '',
'body' => '',
'priority' => '',
'result' => '',
);
my @SubNames =
qw( ResetPW ChangeRole DeleteEvent EditEvent
UpdateEvent DisplayEvents ^InsertNews$ DeleteNews
EditNews UpdateNews DisplayNews);
my $rxhandler = join '|', @SubNames;
my %Handler = (
resetpw => \&resetPW,
changerole => \&changeRole,
deleteevent => \&deleteEvent,
editevent => \&editEvent,
updateevent => \&updateEvent,
displayevents => \&displayEvents,
insertnews => \&insertNews,
deletenews => \&deleteNews,
editnews => \&editNews,
updatenews => \&updateNews,
displaynews => \&displayNews,
);
# -----------------------------------
# Could be external if made into package
#
my $subpage = 'action: ResetPW';
getSub($subpage)->(\%Packet);
exit (0);
# -----------------------------------
## Handlers ...
#
sub getSub {
my ($parse_str) = @_;
$parse_str =~ /($rxhandler)/ && return $Handler{lc $1};
die "Cannot find handler for '$parse_str'"
}
sub resetPW {
my ($P) = @_;
# debug
print "In ResetPW\n" and return;
#
my $email_hash = SQL::reset_password( $P->{foruser} );
CONSOLE::successful_email(
"$email_hash->{fullname}",
"$email_hash->{email1},$email_hash->{email2},
$email_hash->{email3}",
'reset_password@site.org',
'',
'Your MUPD password has been reset',
"Dear $email_hash->{fullname}: Yoursite.org password has been reset to ********'.
Please use the contact form if you have any questions about this. Thank you."
);
HTML::control_button( $P->{sesskey}, 'Administer Security' );
$P->{result} = 'OK'
}
sub changeRole {
my ($P) = @_;
SQL::change_role( $P->{foruser} );
HTML::control_button( $P->{sesskey}, 'Administer Security' );
$P->{result} = 'OK'
}
# calendar event handlers
# ..
sub deleteEvent {
my ($P) = @_;
SQL::delete_event( $P->{foruser} );
HTML::control_button( $P->{sesskey}, 'Manage Events' );
$P->{result} = 'OK'
}
sub editEvent {
my ($P) = @_;
HTML::control_button( $P->{sesskey}, 'Manage Events' );
HTML::manage_event_form( $P->{sesskey}, $P->{foruser} );
$P->{result} = 'OK'
}
sub updateEvent {
my ($P) = @_;
SQL::update_event(
$P->{calid}, $P->{event}, $P->{description}, $P->{place},
$P->{eventdate}, $P->{type}, $P->{uniform}, $P->{required},
$P->{display}, $P->{comments} );
HTML::control_button( $P->{sesskey}, 'Manage Events' );
$P->{result} = 'OK'
}
sub displayEvents {
my ($P) = @_;
my $events_ref = SQL::get_events();
HTML::display_events( $events_ref );
$P->{result} = 'OK'
}
# news item handlers
# ..
sub insertNews {
my ($P) = @_;
SQL::insert_news(
$P->{head}, $P->{first}, $P->{body}, $P->{priority},
$P->{display}, $P->{comments}, $P->{username} );
my $msg = qq(
HEADLINE: $P->{head}
FIRST: $P->{first}
BODY: $P->{body}
COMMENTS: $P->{comments}
USERNAME: $P->{username}
);
SITE::notify_admin( 'News Article Added', $msg );
HTML::control_button( $P->{sesskey}, 'Add News');
$P->{result} = 'OK'
}
sub deleteNews {
my ($P) = @_;
SQL::delete_news( $P->{foruser} );
HTML::control_button( $P->{sesskey}, 'Manage News' );
$P->{result} = 'OK'
}
sub editNews {
my ($P) = @_;
HTML::manage_news_form( $P->{sesskey}, $P->{foruser} );
HTML::control_button( $P->{sesskey}, 'Manage News' );
$P->{result} = 'OK'
}
sub updateNews {
my ($P) = @_;
SQL::update_news(
$P->{newsid}, $P->{head}, $P->{first}, $P->{body},
$P->{priority}, $P->{display}, $P->{comments} );
HTML::control_button( $P->{sesskey}, 'Manage News' );
$P->{result} = 'OK'
}
sub displayNews {
my ($P) = @_;
my $news_ref = SQL::get_news();
HTML::display_news( $news_ref );
$P->{result} = 'OK'
}
__END__
------------------------------
Date: Thu, 21 Jan 2010 04:09:11 -0800 (PST)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: alternatives for branching
Message-Id: <e8734c55-131b-426f-b78b-9fa3f908412a@m16g2000yqc.googlegroups.com>
On Jan 20, 5:38=A0pm, s...@netherlands.com wrote:
> Typically you could make a package out of it.
> The biggest problem is not the branching, its the organization
> of your data. How do you blindly know what to set the data
> before even branching?
The other side of this contains logic very similar to this, something
like this (from memory):
HTML::get_header($var1, $var2);
HTML::get_banner($var3, $var4);
HTML::get_content($page);
HTML::get_footer();
exit(0);
The get_content($page) sub gets the content from the database and
displays the page, depending on the $page argument.
As to the data, I get ALL of the data from the user's browser set as
lexical variables to the package. The top of the application has lines
like this:
use CGI qw(:standard);
my $page =3D param('page');
my $subpage =3D param('subpage');
my $var1 =3D param('var1'');
etc ...
> You need to figure out the data handling before making a package.
> Below lays out organized data for that decision.
I'm going to have to spend some time looking at this. The application
works fine and isn't difficult to maintain, so I don't have an
incentive to rewrite the entire thing. I would rather change this
incrementally over time.
I'd really like to write a script that will generate the proper code
given the source listings as data, but this is a project for a rainy
day.
Thanks for your suggestion, I really appreciate it, CC.
>
> -sln
>
> use strict;
> use warnings;
>
> ## Globals
> #
> my %Packet =3D (
> =A0 'sesskey' =A0 =A0 =3D> '',
> =A0 'foruser' =A0 =A0 =3D> '',
> =A0 'calid' =A0 =A0 =A0 =3D> '',
> =A0 'event' =A0 =A0 =A0 =3D> '',
> =A0 'description' =3D> '',
> =A0 'place' =A0 =A0 =A0 =3D> '',
> =A0 'eventdate' =A0 =3D> '',
> =A0 'type' =A0 =A0 =A0 =A0=3D> '',
> =A0 'uniform' =A0 =A0 =3D> '',
> =A0 'required' =A0 =A0=3D> '',
> =A0 'display' =A0 =A0 =3D> '',
> =A0 'comments' =A0 =A0=3D> '',
> =A0 'newsid' =A0 =A0 =A0=3D> '',
> =A0 'username' =A0 =A0=3D> '',
> =A0 'head' =A0 =A0 =A0 =A0=3D> '',
> =A0 'first' =A0 =A0 =A0 =3D> '',
> =A0 'body' =A0 =A0 =A0 =A0=3D> '',
> =A0 'priority' =A0 =A0=3D> '',
> =A0 'result' =A0 =A0 =A0=3D> '',
> );
> my @SubNames =3D
> =A0 =A0 qw( ResetPW ChangeRole DeleteEvent EditEvent
> =A0 =A0 =A0 =A0 UpdateEvent DisplayEvents ^InsertNews$ DeleteNews
> =A0 =A0 =A0 =A0 EditNews UpdateNews DisplayNews);
>
> my $rxhandler =3D join '|', @SubNames;
>
> my %Handler =3D (
> =A0 resetpw =A0 =A0 =3D> \&resetPW,
> =A0 changerole =A0=3D> \&changeRole,
> =A0 deleteevent =3D> \&deleteEvent,
> =A0 editevent =A0 =3D> \&editEvent,
> =A0 updateevent =3D> \&updateEvent,
> =A0 displayevents =3D> \&displayEvents,
> =A0 insertnews =A0=3D> \&insertNews,
> =A0 deletenews =A0=3D> \&deleteNews,
> =A0 editnews =A0 =A0=3D> \&editNews,
> =A0 updatenews =A0=3D> \&updateNews,
> =A0 displaynews =3D> \&displayNews,
> );
>
> # -----------------------------------
> # Could be external if made into package
> #
> my $subpage =3D 'action: ResetPW';
>
> getSub($subpage)->(\%Packet);
>
> exit (0);
> # -----------------------------------
>
> ## Handlers ...
> #
> sub getSub {
> =A0 my ($parse_str) =3D @_;
> =A0 $parse_str =3D~ /($rxhandler)/ && return $Handler{lc $1};
> =A0 die "Cannot find handler for '$parse_str'"
>
> }
>
> sub resetPW {
> =A0 my ($P) =3D @_;
> =A0 =A0 =A0# debug
> =A0 =A0 =A0 =A0print "In ResetPW\n" and return;
> =A0 =A0 =A0#
> =A0 my $email_hash =3D SQL::reset_password( $P->{foruser} );
> =A0 CONSOLE::successful_email(
> =A0 =A0"$email_hash->{fullname}",
> =A0 =A0"$email_hash->{email1},$email_hash->{email2},
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0$email_hash->{email3}"=
,
> =A0 =A0 'reset_passw...@site.org',
> =A0 =A0 '',
> =A0 =A0 'Your MUPD password has been reset',
> =A0 =A0 "Dear $email_hash->{fullname}: Yoursite.org password has been res=
et to ********'.
> =A0 =A0 =A0 Please use the contact form if you have any questions about t=
his. Thank you."
> =A0 );
> =A0 HTML::control_button( $P->{sesskey}, 'Administer Security' );
> =A0 $P->{result} =3D 'OK'
>
> }
>
> sub changeRole {
> =A0 my ($P) =3D @_;
> =A0 SQL::change_role( $P->{foruser} );
> =A0 HTML::control_button( $P->{sesskey}, 'Administer Security' );
> =A0 $P->{result} =3D 'OK'
>
> }
>
> # calendar event handlers
> # ..
> sub deleteEvent {
> =A0 my ($P) =3D @_;
> =A0 SQL::delete_event( $P->{foruser} );
> =A0 HTML::control_button( $P->{sesskey}, 'Manage Events' );
> =A0 $P->{result} =3D 'OK'
>
> }
>
> sub editEvent {
> =A0 my ($P) =3D @_;
> =A0 HTML::control_button( $P->{sesskey}, 'Manage Events' );
> =A0 HTML::manage_event_form( $P->{sesskey}, $P->{foruser} );
> =A0 $P->{result} =3D 'OK'
>
> }
>
> sub updateEvent {
> =A0 my ($P) =3D @_;
> =A0 SQL::update_event(
> =A0 =A0 =A0$P->{calid}, $P->{event}, $P->{description}, $P->{place},
> =A0 =A0 =A0$P->{eventdate}, $P->{type}, $P->{uniform}, $P->{required},
> =A0 =A0 =A0$P->{display}, $P->{comments} );
> =A0 HTML::control_button( $P->{sesskey}, 'Manage Events' );
> =A0 $P->{result} =3D 'OK'
>
> }
>
> sub displayEvents {
> =A0 my ($P) =3D @_;
> =A0 my $events_ref =3D SQL::get_events();
> =A0 HTML::display_events( $events_ref );
> =A0 $P->{result} =3D 'OK'
>
> }
>
> # news item handlers
> # ..
> sub insertNews {
> =A0 my ($P) =3D @_;
> =A0 SQL::insert_news(
> =A0 =A0 =A0 =A0$P->{head}, $P->{first}, $P->{body}, $P->{priority},
> =A0 =A0 =A0 =A0$P->{display}, $P->{comments}, $P->{username} );
> =A0 my $msg =3D qq(
> =A0 =A0 =A0 =A0HEADLINE: $P->{head}
> =A0 =A0 =A0 =A0FIRST: $P->{first}
> =A0 =A0 =A0 =A0BODY: $P->{body}
> =A0 =A0 =A0 =A0COMMENTS: $P->{comments}
> =A0 =A0 =A0 =A0USERNAME: $P->{username}
> =A0 );
> =A0 SITE::notify_admin( 'News Article Added', $msg );
> =A0 HTML::control_button( $P->{sesskey}, 'Add News');
> =A0 $P->{result} =3D 'OK'
>
> }
>
> sub deleteNews {
> =A0 my ($P) =3D @_;
> =A0 SQL::delete_news( $P->{foruser} );
> =A0 HTML::control_button( $P->{sesskey}, 'Manage News' );
> =A0 $P->{result} =3D 'OK'
>
> }
>
> sub editNews {
> =A0 my ($P) =3D @_;
> =A0 HTML::manage_news_form( $P->{sesskey}, $P->{foruser} );
> =A0 HTML::control_button( $P->{sesskey}, 'Manage News' );
> =A0 $P->{result} =3D 'OK'
>
> }
>
> sub updateNews {
> =A0 my ($P) =3D @_;
> =A0 SQL::update_news(
> =A0 =A0 =A0 =A0 $P->{newsid}, $P->{head}, $P->{first}, $P->{body},
> =A0 =A0 =A0 =A0 $P->{priority}, $P->{display}, $P->{comments} );
> =A0 HTML::control_button( $P->{sesskey}, 'Manage News' );
> =A0 $P->{result} =3D 'OK'
>
> }
>
> sub displayNews {
> =A0 my ($P) =3D @_;
> =A0 my $news_ref =3D SQL::get_news();
> =A0 HTML::display_news( $news_ref );
> =A0 $P->{result} =3D 'OK'}
>
> __END__
------------------------------
Date: Wed, 20 Jan 2010 02:40:28 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: FAQ 7.28 How do I clear a package?
Message-Id: <s17i27-1a8.ln1@osiris.mauzo.dyndns.org>
Quoth Keith Thompson <kst-u@mib.org>:
> brian d foy <brian.d.foy@gmail.com> writes:
> > In article <lnzl4b9ucw.fsf@nuthaus.mib.org>, Keith Thompson
> > <kst-u@mib.org> wrote:
> >> I think this answer would be improved by explaining what it means to
> >> "clear" a package, and why one would want to do so (I can't think of a
> >> good reason off the top of my head).
> >
> > If you don't know what it is, that's probably a good thing. :)
>
> Well, I can tell what it is by looking at the code, but why would you
> want to do that? Is the Question really Asked all that Frequently?
It's usually (or, perhaps, was usually) asked as part of 'how can I
unload a module' or 'how can I reload a module if the file on disk has
changed'. Since the answer is 'you can't, really; there are various
hacks, including clearing out the package's symbol table, but since they
tend to have unexpected side-effects (including, under some
circumstances, segfaulting perl) it's not worth trying unless you know
enough perlguts to figure it out yourself' I would suggest the answer
doesn't really belong in the FAQ. It's not as though
Symbol::delete_package is at all hard to find.
Ben
------------------------------
Date: Thu, 21 Jan 2010 02:25:01 -0800 (PST)
From: Marc Girod <marc.girod@gmail.com>
Subject: macros: return or exit
Message-Id: <ad8ecb2f-4c8b-4ff8-b2cc-7ed74595077b@c29g2000yqd.googlegroups.com>
Hello,
I consider making a change to a module, which will significantly alter
its client interface.
Until now, client functions could exit, die, or exec: no further
action was meant to take place.
Alternatively, they could return 0, and a generic behaviour would
follow.
I want now to introduce a loop, i.e. to retain control.
I control one such client, and am not sure whether there are actually
more than one other.
My problem is however to maintain a branch of the module and its known
clients, at least in order to investigate it and possibly to get
feedback. This could take a year or so. Or more.
What I did in the main loop, is:
< my $rc = &$cmd(@ARGV);
< # ... and exit unless it returned zero.
< exit $rc if $rc;
---
> my ($rc, $done) = &$cmd(@ARGV);
> # ... and return unless it returned zero and not done.
> return $rc if $done || $rc;
The change in the client code is thus ( a couple of examples out of
many):
< exit $?;
---
> return($?, 'done');
or
< $lsp->exec;
---
> return($lsp->system, 'done');
etc.
What are my best options to handle this?
I could sketch a few overrides such as e.g. for 'exit':
$legacy? exit $_ : return($_, 'done');
...but this ought to be a macro, not a function, for 'return' to
return to the right context.
What should I look for in CPAN or in the FAQ?
How to make my question shorter so that it fits in such a query?
If you are curious (you are fully entitled to), the modules are (on
CPAN):
ClearCase::Wrapper (the base module)
ClearCase::Wrapper::MGi (my own client)
ClearCase::Wrapper::DSB (the other known client)
Thanks,
Marc
------------------------------
Date: Thu, 21 Jan 2010 13:14:01 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: macros: return or exit
Message-Id: <ph0m27-o4h1.ln1@osiris.mauzo.dyndns.org>
Quoth Marc Girod <marc.girod@gmail.com>:
>
> I consider making a change to a module, which will significantly alter
> its client interface.
> Until now, client functions could exit, die, or exec: no further
> action was meant to take place.
> Alternatively, they could return 0, and a generic behaviour would
> follow.
>
> I want now to introduce a loop, i.e. to retain control.
> I control one such client, and am not sure whether there are actually
> more than one other.
>
> My problem is however to maintain a branch of the module and its known
> clients, at least in order to investigate it and possibly to get
> feedback. This could take a year or so. Or more.
It's not clear what procedure you're trying to follow here. Are you
going to create your own fork of the module, and possibly try to feed
the changes upstream, or are you trying to find a way to avoid that?
> What I did in the main loop, is:
>
> < my $rc = &$cmd(@ARGV);
The syntax $cmd->(@ARGV) is usually clearer, and generalises better to
situations like $cmd{$which}->(@ARGV).
> < # ... and exit unless it returned zero.
> < exit $rc if $rc;
> ---
> > my ($rc, $done) = &$cmd(@ARGV);
> > # ... and return unless it returned zero and not done.
> > return $rc if $done || $rc;
>
> The change in the client code is thus ( a couple of examples out of
> many):
>
> < exit $?;
> ---
> > return($?, 'done');
>
> or
>
> < $lsp->exec;
> ---
> > return($lsp->system, 'done');
>
> etc.
> What are my best options to handle this?
One option is to accept that the module you are using will terminate the
process, and fork before calling it in order to retain control. You will
need to think carefully about whether the exit calls are going to do
anything unpleasant to your filehandle buffers.
> I could sketch a few overrides such as e.g. for 'exit':
>
> $legacy? exit $_ : return($_, 'done');
>
> ...but this ought to be a macro, not a function, for 'return' to
> return to the right context.
> What should I look for in CPAN or in the FAQ?
Perl doesn't have macros, I'm afraid. If you can afford to maintain a
fork of the module, a simple search-and-replace in your text editor
would seem the easiest option.
Otherwise, one nasty option (and you're pretty much into nasty options
if you need to wrap a module with such an unpleasant interface) would be
to use the (undocumented and unexported) Want::double_return function
provided you the Want module. This sets up the next return to return
'twice', so you could do something like
require Want;
*Nasty::Module::exit = sub {
my ($rv) = @_;
Want::double_return();
return $rv, "done";
};
This would need to be done before loading Nasty::Module, so the override
gets properly picked up.
Ben
------------------------------
Date: Wed, 20 Jan 2010 22:25:36 -0800 (PST)
From: Alexander Jack <abu.4000@gmail.com>
Subject: select the correct answer
Message-Id: <5f8933fb-6495-454d-a822-172ee8263234@r24g2000yqd.googlegroups.com>
I came across this question in my office ,Im having my own idea
regarding this , so please tell your views and explanation if
possible
#! /usr/local/bin/perl -Tw
sub foo {
my $data = shift;
if ($data =~ /^([-\w.]*)$/) {
return $1;
} else {
die "Bad data in $data\n";
}
}
my $line = <STDIN>;
$line = foo($line);
Question: When is code similar to the above code used in a perl
program ?
Choices:
a. When most data is required as a scalar
b. When running setuid perl scripts that must securely use user-
supplied data
c. When preparing a function that will eventually check data for
specific criteria
d. When wanting to have a function return a string form of a parameter
e. When any string data must contain at least one character
------------------------------
Date: Thu, 21 Jan 2010 08:48:39 +0100
From: ace <ace@nosite.com>
Subject: Re: select the correct answer
Message-Id: <hj90so$p6r$1@ss408.t-com.hr>
Alexander Jack wrote:
> Choices:
> a. When most data is required as a scalar
> b. When running setuid perl scripts that must securely use user-
> supplied data
> c. When preparing a function that will eventually check data for
> specific criteria
> d. When wanting to have a function return a string form of a parameter
> e. When any string data must contain at least one character
c
------------------------------
Date: Thu, 21 Jan 2010 07:16:31 -0600
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: select the correct answer
Message-Id: <slrnhlgkn2.n3u.tadmc@tadbox.sbcglobal.net>
Alexander Jack <abu.4000@gmail.com> wrote:
> Question: When is code similar to the above code used in a perl
> program ?
>
> Choices:
> a. When most data is required as a scalar
> b. When running setuid perl scripts that must securely use user-
> supplied data
> c. When preparing a function that will eventually check data for
> specific criteria
> d. When wanting to have a function return a string form of a parameter
> e. When any string data must contain at least one character
Cheater!
Do your own homework.
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
------------------------------
Date: Thu, 21 Jan 2010 01:20:51 -0800 (PST)
From: Marc Girod <marc.girod@gmail.com>
Subject: Strip control characters in a file
Message-Id: <0ff4da0e-81fd-4518-844a-dcdb77e3736b@b10g2000yqa.googlegroups.com>
Hello,
I was asked a way to strip control characters from a text file.
Soon, it became clear that newlines must be kept, as well as
(probably) tabs.
The context was however unix only.
Inspired in part by recent posts in this group, I came up with the
following one-liner:
perl -pi2 -e 'BEGIN{$rep{chr($_)}=q() for 0..31,127;$rep{chr(10)}=chr
(10)}s/([[:cntrl:]])/$rep{$1}/g' /tmp/fff
...assuming the file was /tmp/fff, and keeping a backup of it.
I would now humbly turn to you for critique and improvements.
Thanks,
marc
------------------------------
Date: Thu, 21 Jan 2010 04:28:20 -0500
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: Strip control characters in a file
Message-Id: <87hbqfst6z.fsf@quad.sysarch.com>
>>>>> "MG" == Marc Girod <marc.girod@gmail.com> writes:
MG> I was asked a way to strip control characters from a text file.
MG> Soon, it became clear that newlines must be kept, as well as
MG> (probably) tabs. The context was however unix only.
MG> Inspired in part by recent posts in this group, I came up with the
MG> following one-liner:
MG> perl -pi2 -e 'BEGIN{$rep{chr($_)}=q() for 0..31,127;$rep{chr(10)}=chr
MG> (10)}s/([[:cntrl:]])/$rep{$1}/g' /tmp/fff
MG> ...assuming the file was /tmp/fff, and keeping a backup of it.
MG> I would now humbly turn to you for critique and improvements.
use tr///. untested (need to check the chars):
perl -pi2 -e 'tr/0x00-0x090x11-0x1f//d'
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Wed, 20 Jan 2010 16:34:59 -0500
From: sreservoir <sreservoir@gmail.com>
Subject: Re: Variable set to 0
Message-Id: <hj7su2$d9q$1@speranza.aioe.org>
On 1/19/2010 3:32 PM, Peter J. Holzer wrote:
> On 2010-01-19 02:40, sreservoir<sreservoir@gmail.com> wrote:
>> ~% perl -wle'print !1'
>> Use of uninitialized value in print at -e line 1.
>
> I suspect that your shell (csh?) is fooling you. If !1 is interpreted as
> a history expansion and command 1 was empty the command really reads:
>
> ~% perl -wle'print '
>
> which will produce the warning you see.
~% echo $SHELL
/bin/csh
gah!
--
"Six by nine. Forty two."
"That's it. That's all there is."
"I always thought something was fundamentally wrong with the universe"
------------------------------
Date: Wed, 20 Jan 2010 16:37:04 -0500
From: sreservoir <sreservoir@gmail.com>
Subject: Re: Variable set to 0
Message-Id: <hj7t1v$d9q$2@speranza.aioe.org>
On 1/20/2010 4:34 PM, sreservoir wrote:
> On 1/19/2010 3:32 PM, Peter J. Holzer wrote:
>> On 2010-01-19 02:40, sreservoir<sreservoir@gmail.com> wrote:
>>> ~% perl -wle'print !1'
>>> Use of uninitialized value in print at -e line 1.
>>
>> I suspect that your shell (csh?) is fooling you. If !1 is interpreted as
>> a history expansion and command 1 was empty the command really reads:
>>
>> ~% perl -wle'print '
>>
>> which will produce the warning you see.
>
> ~% echo $SHELL
> /bin/csh
>
> gah!
>
in sh:
~% perl -wle'print !1'
~%
--
"Six by nine. Forty two."
"That's it. That's all there is."
"I always thought something was fundamentally wrong with the universe"
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V11 Issue 2780
***************************************