Re: [PLUG] question re: perl diamond operator

Mark M. Hoffman on 25 Jun 2009 12:07:40 -0700


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] question re: perl diamond operator


Hi:
* Paul W. Roach III <paul@isaroach.com> [2009年06月25日 14:23:22 -0400]:
> Yeah...
> *
> #!/usr/bin/perl
> *
> foreach my $file (@ARGV) {
> ** open(FILE, $file) || die "couldnt open file $file: $!\n";
> ** chomp(my @lines = <FILE>);
> ** close(FILE);
> *
> ** print @lines;
> }
Yes obviously... but then I lose the ability to (also) do this:
	$ some_command | ./example.pl
I asked about the diamond operator, specifically, for good reason.
> On Thu, Jun 25, 2009 at 1:14 PM, Morgan Jones <[1]morgan@morganjones.org>
> wrote:
> 
> You could also loop through %ARGV and test each value.
> 
> [2]http://en.wikipedia.org/wiki/There%27s_more_than_one_way_to_do_it
> -morgan
> 
> On Jun 25, 2009, at 1:03 PM, Mark M. Hoffman wrote:
> 
> > Hi:
> >
> > * Morgan Jones <[3]morgan@morganjones.org> [2009年06月25日 12:47:38
> -0400]:
> >> #! /usr/bin/perl -w
> >>
> >> if (! -f $ARGV[0]) {
> >> * * print "$ARGV[0] does not exist\n";
> >> * * exit 1;
> >> }
> >>
> >> while (<>) {
> >> * * *chomp;
> >> * * *print $_;
> >> }
> >
> > Sure I could do that, but consider...
> >
> > * * * $ ./example.pl this_file_exists but_not_this_one
> >
> > It's a bit indirect, but I've solved it a completely different way.
> > I added
> > the following line near the top... it installs a warning handler
> > which just
> > converts all warnings into fatal errors. *Good enough for me.
> >
> > * * * local $SIG{__WARN__} = sub { die $_[0] };
> >
> >> On Jun 25, 2009, at 12:24 PM, Mark M. Hoffman wrote:
> >>
> >>> Hi all:
> >>>
> >>> Given the following trivial script:
> >>>
> >>>>> #! /usr/bin/perl -w
> >>>>>
> >>>>> while (<>) {
> >>>>> * chomp;
> >>>>> * print $_;
> >>>>> }
> >>>
> >>> * * $ ./example.pl foo
> >>> * * Can't open foo: No such file or directory at ./example.pl line
> 3.
> >>>
> >>> So far so good; the file foo really does not exist.
> >>>
> >>> * * $ echo $?
> >>> * * 0
> >>>
> >>> Really? *I want the exit status to be non-zero here. *I've looked
> >>> through the
> >>> perl documentation; I can't see any way to get the diamond operator
> >>> to do what
> >>> I want in this case. *I would appreciate any suggestions from perl
> >>> experts.
> >
Regards,
-- 
Mark M. Hoffman
mhoffman@lightlink.com
___________________________________________________________________________
Philadelphia Linux Users Group -- http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion -- http://lists.phillylinux.org/mailman/listinfo/plug



AltStyle によって変換されたページ (->オリジナル) /