From justin.denick at gmail.com Fri Mar 2 10:28:28 2007
From: justin.denick at gmail.com (Justin Denick)
Date: Fri, 2 Mar 2007 13:28:28 -0500
Subject: [grand-rapids-pm-list] Open a file based on content
Message-ID: <81e08d920703021028l17d98c0cw7e082d697821393e@mail.gmail.com>
I have a bunch of files with garbage names, but they can be identified by
their header.
In bash I can say
x=0
pattern="pattern"
files=`grep -l $pattern list-of-files`
for this in $files do;
if [ $x -eq 0 ]
sed s/\~/\\r\\n/g $files > name.txt
else
sed s/\~/\\r\\n/g $files > name.txt$x
x=`expr $x + 1`
fi
done
But I want learn how to do this in perl
so it would be something like (and please pardon my ignorance)
my $x = 0;
my $pattern = "pattern";
open DH, "path" or die;
my @files = (readdir DH);
my @found = grep($pattern, @files);
for my $source (@found) {
my destination = "$source.new"
open INPUT, "<$source" or die;
open OUTPUT ">$destination" or die;
while () {
s/\176/\012\015/g;
print OUTPUT $_;
rename $destination, $source;
}
}
--
In vino veritas.
[In wine there is truth.]
-- Pliny
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/grand-rapids-pm-list/attachments/20070302/dcf8bc4e/attachment.html
From justin.denick at gmail.com Fri Mar 2 10:30:01 2007
From: justin.denick at gmail.com (Justin Denick)
Date: Fri, 2 Mar 2007 13:30:01 -0500
Subject: [grand-rapids-pm-list] Open a file based on content
In-Reply-To: <81e08d920703021028l17d98c0cw7e082d697821393e@mail.gmail.com>
References: <81e08d920703021028l17d98c0cw7e082d697821393e@mail.gmail.com>
Message-ID: <81e08d920703021030h418d9531t5c2bfbebc1485713@mail.gmail.com>
On 3/2/07, Justin Denick wrote:
>
> I have a bunch of files with garbage names, but they can be identified by
> their header.
> In bash I can say
>
> x=0
> pattern="pattern"
> files=`grep -l $pattern list-of-files`
>
> for this in $files do;
> if [ $x -eq 0 ]
> sed s/\~/\\r\\n/g $files > name.txt
add one to $x here
x=`expr $x + 1`
else
> sed s/\~/\\r\\n/g $files > name.txt$x
>
> fi
> done
>
>
> But I want learn how to do this in perl
> so it would be something like (and please pardon my ignorance)
>
> my $x = 0;
> my $pattern = "pattern";
>
> open DH, "path" or die;
> my @files = (readdir DH);
> my @found = grep($pattern, @files);
> for my $source (@found) {
> my destination = "$source.new"
> open INPUT, "<$source" or die;
> open OUTPUT ">$destination" or die;
> while () {
> s/\176/\012\015/g;
> print OUTPUT $_;
> rename $destination, $source;
> }
>
> }
>
>
>
>
> --
> In vino veritas.
> [In wine there is truth.]
> -- Pliny
--
In vino veritas.
[In wine there is truth.]
-- Pliny
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/grand-rapids-pm-list/attachments/20070302/4ae31aca/attachment.html
From tobert at gmail.com Tue Mar 6 07:29:33 2007
From: tobert at gmail.com (Al Tobey)
Date: Tue, 6 Mar 2007 10:29:33 -0500
Subject: [grand-rapids-pm-list] [GRLUG] Open a file based on Content
[Perl]
In-Reply-To: <81e08d920703060511u74d0f892p16f3114b3ad33ba3@mail.gmail.com>
References: <81e08d920703060511u74d0f892p16f3114b3ad33ba3@mail.gmail.com>
Message-ID: <5ac7acb10703060729y422e9f70kdcb9848d6769382b@mail.gmail.com>
On 3/6/07, Justin Denick wrote:
> This is a cross posted message, if that offends you than seek consoling.
>
> I have the need to open files and manipulate them into helping me take over
> the world. Muhahahah.
> I can do it in bash, but I really want to learn perl, so hopefully one of
> you guys or girls
> could help. (do we have any women on this list?)
>
> Here's how I gitrdun in bash.
>
> > x=0
> > pattern="pattern"
> > files=`grep -l $pattern list-of-files`
> >
> > for this in $files do;
> > if [ $x -eq 0 ]
> > sed s/\~/\\r\\n/g $files > name.txt
> >
>
>
> > x=`expr $x + 1`
> >
>
>
> > else
> > sed s/\~/\\r\\n/g $files > name.txt$x
> >
> > fi
> > done
>
>
>
> But I want learn how to do this in perl
> so it would be something like (and please pardon my ignorance)
>
> > my $x = 0;
> > my $pattern = "pattern";
> >
> > open DH, "path" or die;
> > my @files = (readdir DH);
> > my @found = grep($pattern, @files);
> > for my $source (@found) {
> > my destination = "$source.new"
> > open INPUT, "<$source" or die;
> > open OUTPUT ">$destination" or die;
> > while () {
> > s/\176/\012\015/g;
> > print OUTPUT $_;
> > rename $destination, $source;
> > }
> >
> > }
>
> Thanks in advance for your help. '
grep() in perl is quite different from grep(1) in Unix. It takes a
statement and a list of scalars and runs the block across them
returning items for which the statement evaluated to true. So,
grep /~/, @lines;
will return a list of lines that contain a tilde. Check out "perldoc -f grep".
This is a quick-n-dirty, completely untested, and unverified version.
It also tries to be efficient about opening files multiple times,
which the shell version can't do. I highly recommend the Perl
Cookbook from O'Reilly, since it is geared towards "Git-R-Done" rather
than a more academic approach to learning Perl.
#!/usr/bin/perl
use File::Find;
# see perldoc -f qr
my $pattern = qr/pattern/;
sub wanted {
# recent versions of perl allow/prefer "my $fh" over barewords
open my $fh, "< $File::Find::name";
return unless ( $fh ); # skip this file if open() failed
while ( my $line = <$fh> ) {
# only scan the file up to the first match, then immediately jump to
# processing it then bail out of this loop
if ( $line =~ /$pattern/ ) {
process( $fh, $File::Find::name );
last; # exit the loop now that the file is processed
}
}
}
sub process {
my( $fh, $filename ) = @_;
# move the file handle back to the top of the file -- it may be more
# efficient for large files to only seek backwards a little bit, but this
# is easier to follow for now
seek $fh, 0, 0;
open my $out, "> $filename.new"
or die "Unable to open $filename.new for writing: $!";
while ( my $line = <$fh> ) {
$line =~ s/\176/\012\015/g;
print $out $line;
}
close $out;
close $fh;
rename "$filename.new" $filename;
}
# this will recurse a directory tree
find({ wanted => \&wanted, no_chdir => 1 }, '.' );
-Al Tobey
From rdavis at fbinsmi.com Tue Mar 13 10:58:34 2007
From: rdavis at fbinsmi.com (Davis, Ryan)
Date: Tue, 13 Mar 2007 13:58:34 -0400
Subject: [grand-rapids-pm-list] Username from Active Directory
Message-ID:
I am trying to have a perl cgi script running on our intranet obtain the username/userid from Active Directory.
Our environment here has AD used throughout the enterprise. The web server involved is IHS (IBM Http Server - based on Apache) running on Win2000. Again this is only used by internal users.
I've written this as a script using Win32::OLE. Here's the code:
print ("Content-Type: text/html\n\n");
my $sysinfo = Win32::OLE->new('ADSystemInfo') || print (" Error line 18 ");
my $username=$sysinfo->{UserName};
my $adsuser = Win32::OLE->GetObject(" LDAP://$username") || print (" Error line 24 ");
my $name = $adsuser->{cn};
print ("Username:$username Name:$name ");
print ("");
When I run this on my own workstation everything works as I would like. When I run it on the server as a cgi, the username comes back blank. I suspect that the cgi is running without AD information available, but am told otherwise by technical resources here.
Is there any way to get the username in this situation?
Ryan Davis
Web & Agency Support
x1540
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/grand-rapids-pm-list/attachments/20070313/a3d48c2e/attachment.html
From tobert at gmail.com Wed Mar 28 10:40:50 2007
From: tobert at gmail.com (Al Tobey)
Date: Wed, 28 Mar 2007 09:40:50 -0800
Subject: [grand-rapids-pm-list] This week's meeting is canceled
Message-ID: <5ac7acb10703281040s6c153228r43823c48b87dd4ff@mail.gmail.com>
Due to a lack of volunteers to speak and my fall-backs all being busy
with other endeavors, I am cancelling this week's Perl Mongers
meeting.
We are on for April, but I still have a couple loose ends to pull
together so I'll announce that later. There are no plans for what
to do after that, but I'd be thrilled to get some volunteers ;)
Think about it -- all ideas are welcome.
-Al
From tobert at gmail.com Thu Mar 29 08:29:53 2007
From: tobert at gmail.com (Al Tobey)
Date: Thu, 29 Mar 2007 07:29:53 -0800
Subject: [grand-rapids-pm-list] Fwd: [Chicago-talk] April meetings: 101
things every Perl programmer should know
In-Reply-To: <1175181776.1BEAc0.2039@rocket>
References: <1175181776.1BEAc0.2039@rocket>
Message-ID: <5ac7acb10703290829y7e2de0d1of6655570eeb60505@mail.gmail.com>
In case anybody is interested ...
---------- Forwarded message ----------
From: andy at petdance.com
Date: Mar 29, 2007 7:22 AM
Subject: [Chicago-talk] April meetings: 101 things every Perl
programmer should know
To: chicago-talk at pm.org, chicago-announce at pm.org
http://rakudo.org/chicago-pm/index.cgi?101_things_every_perl_programmer_should_know
Andy Lester and Pete Krawczyk present a fun-filled night of
fascinating features and facts that you may not know about Perl, CPAN
and the Perl community, but definitely should. The evening is aimed at
beginners and intermediate Perl programmers, but experts may just
learn a thing or two as well. Our goal is to blow your mind at least
once before the night is through.
You'll have two opportunities to hear it:
April 10th, 2007, 7pm at Illinois Institute of Technology, Rice Campus
in Wheaton -- No RSVP required
April 24th, 2007, 7pm at Performics in the city -- RSVP required by
noon to the email address "pkrawczyk" at the site "performics.com".
As with all Chicago Perl Mongers meetings, everyone is welcome,
whether or not you consider yourself a member. We look forward to
seeing you there!
_______________________________________________
Chicago-talk mailing list
Chicago-talk at pm.org
http://mail.pm.org/mailman/listinfo/chicago-talk
From andy at petdance.com Thu Mar 29 08:33:26 2007
From: andy at petdance.com (Andy Lester)
Date: Thu, 29 Mar 2007 10:33:26 -0500
Subject: [grand-rapids-pm-list] Fwd: [Chicago-talk] April meetings: 101
things every Perl programmer should know
In-Reply-To: <5ac7acb10703290829y7e2de0d1of6655570eeb60505@mail.gmail.com>
References: <1175181776.1BEAc0.2039@rocket>
<5ac7acb10703290829y7e2de0d1of6655570eeb60505@mail.gmail.com>
Message-ID:
On Mar 29, 2007, at 10:29 AM, Al Tobey wrote:
> In case anybody is interested ...
Heck, we might even bring it to Grand Rapids. I haven't been to GR
in a while.
--
Andy Lester => andy at petdance.com => www.petdance.com => AIM:petdance