Python for Perl programmers
everythingsysadmin.com
Python for Perl programmers
1–10 of 40 posts
Re: Python for Perl programmers
#2Re: Python for Perl programmers
#3In perl,
> my ($a, $b) = '12' =~ /(\d)(\d)/;
In python,
> (a, b) = re.match('(\d)(\d)', '12').groups()
Note the python code here will throw an AttributeError if the match fails, whereas the perl will just assign undef to the variables.
Re: Python for Perl programmers
#4It actually took me 30 minutes today to figure out how to do:
text=open('file.txt').read()
without creating 3-5 lines of code which seems excessive for a simple operation.
There are apparently 20 ways to do it in Perl and they need their own CPAN library for it :-( [1]
[1] http://search.cpan.org/~drolsky/File-Slurp-9999.13/extras/sl...
Re: Python for Perl programmers
#5Re: Python for Perl programmers
#6Not mentioned is the common idiom of assigning regexp matches. In perl, > my ($a, $b) = '12' =~ /(\d)(\d)/; In python, > (a, b) = re.match('(\d)(\d)', '12').groups() Note the python code here will throw an AttributeError if the match fails, whereas the perl will just assign undef to the variables.
> obj = re.match('(?P\d\d)(?P\w+)')
> id = obj.group('id')
> name = obj.group('name')Re: Python for Perl programmers
#7Can someone please make a Perl for Python programmers? It actually took me 30 minutes today to figure out how to do: text=open('file.txt').read() without creating 3-5 lines of code which seems excessive for a simple operation. There are apparently 20 ways to do it in Perl and they need their own CPAN library for it :-( [1] [1] http://search.cpan.org/~drolsky/File-Slurp-9999.13/extras/sl...
Perl isn't batteries included, but its packages are usually actually tested on multiple versions. This allows for the community to move on its own. There is no dictator, by design. Try #perl for help as well, or the package channel if there is one.
Re: Python for Perl programmers
#8Can someone please make a Perl for Python programmers? It actually took me 30 minutes today to figure out how to do: text=open('file.txt').read() without creating 3-5 lines of code which seems excessive for a simple operation. There are apparently 20 ways to do it in Perl and they need their own CPAN library for it :-( [1] [1] http://search.cpan.org/~drolsky/File-Slurp-9999.13/extras/sl...
my $file_handle = open(file.txt)
my $text;
while (read $file_handle, $buff, 1024) { $text = $buff }
read returns the number of characters read (0 when it's finished which will end the loop). I don't know of any generic "read" function, so it's up to you to define how many characters at a time and give the $buffer scalar to read into. it's just a different way of doing things
norkakn is a better resource than this comment
Re: Python for Perl programmers
#9 However, most Python programmers tend to just read the
entire file into one huge string and process it that way.
I feel funny doing that. Having used machines with very
limited amounts of RAM, I tend to try to keep my file
processing to a single line at a time. However, that
method is going the way of the dodo.
contents = file('filename.txt').read()
all_input = sys.stdin.read()
This is not the correct way to do things. The first example given is better. for line in file('filename.txt'):
print line
Generators are a beautiful thing [1][2][1] http://stackoverflow.com/questions/519633/lazy-method-for-re...
Re: Python for Perl programmers
#10Can someone please make a Perl for Python programmers? It actually took me 30 minutes today to figure out how to do: text=open('file.txt').read() without creating 3-5 lines of code which seems excessive for a simple operation. There are apparently 20 ways to do it in Perl and they need their own CPAN library for it :-( [1] [1] http://search.cpan.org/~drolsky/File-Slurp-9999.13/extras/sl...
Yea, my $file_handle = open(file.txt) my $text; while (read $file_handle, $buff, 1024) { $text = $buff } read returns the number of characters read (0 when it's finished which will end the loop). I don't know of any generic "read" function, so it's up to you to define how many characters at a time and give the $buffer scalar to read into. it's just a different way of doing things norkakn is a better resource than thi…
my $text = do { local( @ARGV, $/ ) = $file ; } ;