Reading/writing binary
Hi all, I would like to write binary values to disk (as well as read them) but don't know how to do it. In C-speak, something like this: unsigned int foo = 42; fwrite (&foo, sizeof (unsigned int), 1, stdout); I think the answer involves something with pack and unpack, but I'm completely lost as I have no experience with either. The closest I got was my $decimal_number = 42; my $binary_number = unpack("B32", pack("N", $decimal_number)); print "Decimal number " . $decimal_number . " is " . $binary_number . " in binary.\n\n"; which I've taken from http://www.linuxconfig.org/Perl_Programming_Tutorial. This doesn't work since it's printing the number "42" in text binary; but I think it is close... Or I might be barking up the wrong tree and I should be thinking about "fprintf" somehow... Does anyone have any ideas? Thank you! Ray
Re: Reading/writing binary
On Thu, May 7, 2009 at 05:45, Raymond Wan wrote: > Hi all, > > I would like to write binary values to disk (as well as read them) but don't > know how to do it. In C-speak, something like this: > > unsigned int foo = 42; > fwrite (&foo, sizeof (unsigned int), 1, stdout); > > I think the answer involves something with pack and unpack, but I'm > completely lost as I have no experience with either. The closest I got was > > my $decimal_number = 42; > my $binary_number = unpack("B32", pack("N", $decimal_number)); > print "Decimal number " . $decimal_number . " is " . $binary_number . > " in binary.\n\n"; > > which I've taken from > http://www.linuxconfig.org/Perl_Programming_Tutorial. This doesn't > work since it's printing the number "42" in text binary; but > I think it is close... Or I might be barking up the wrong tree and I should > be thinking about "fprintf" somehow... > > Does anyone have any ideas? Thank you! > > Ray > You use pack to create a binary value and unpack to read a binary value. So, to write a file containing three 32 bit integers you say #!/usr/bin/perl use strict; use warnings; my $file = "/tmp/3ints"; #the :raw here tells Perl that the file will #be binary open my $fh, ">:raw", $file or die "could not open $file: $!\n"; print $fh pack "lll", 1230, -897, 20; close $fh or die "could not close $file: $!\n"; my $size = (stat $file)[7]; print "$file contains $size bytes\n"; And then to read the file, you say: #!/usr/bin/perl use strict; use warnings; my $file = "/tmp/3ints"; my $size = 12; #the :raw here tells Perl that the file will #be binary open my $fh, "<:raw", $file or die "could not open $file: $!\n"; sysread($fh, my $buffer, $size) == $size or die "did not read $size bytes from $file\n"; close $fh or die "could not close $file: $!\n"; print "the ints are ", join(", ", unpack "lll", $buffer), "\n"; -- Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Reading/writing binary
Chas. Owens wrote: On Thu, May 7, 2009 at 05:45, Raymond Wan wrote: I would like to write binary values to disk (as well as read them) but don't know how to do it. In C-speak, something like this: unsigned int foo = 42; fwrite (&foo, sizeof (unsigned int), 1, stdout); I think the answer involves something with pack and unpack, but I'm completely lost as I have no experience with either. The closest I got was my $decimal_number = 42; my $binary_number = unpack("B32", pack("N", $decimal_number)); print "Decimal number " . $decimal_number . " is " . $binary_number . " in binary.\n\n"; which I've taken from http://www.linuxconfig.org/Perl_Programming_Tutorial. This doesn't work since it's printing the number "42" in text binary; but I think it is close... Or I might be barking up the wrong tree and I should be thinking about "fprintf" somehow... Does anyone have any ideas? Thank you! You use pack to create a binary value and unpack to read a binary value. So, to write a file containing three 32 bit integers you say #!/usr/bin/perl use strict; use warnings; my $file = "/tmp/3ints"; #the :raw here tells Perl that the file will #be binary open my $fh, ">:raw", $file or die "could not open $file: $!\n"; print $fh pack "lll", 1230, -897, 20; close $fh or die "could not close $file: $!\n"; my $size = (stat $file)[7]; print "$file contains $size bytes\n"; And then to read the file, you say: #!/usr/bin/perl use strict; use warnings; my $file = "/tmp/3ints"; my $size = 12; #the :raw here tells Perl that the file will #be binary open my $fh, "<:raw", $file or die "could not open $file: $!\n"; sysread($fh, my $buffer, $size) == $size or die "did not read $size bytes from $file\n"; Shouldn't that be either: sysopen ... sysread ... Or: open ... read ... ? close $fh or die "could not close $file: $!\n"; print "the ints are ", join(", ", unpack "lll", $buffer), "\n"; John -- Those people who think they know everything are a great annoyance to those of us who do.-- Isaac Asimov -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Reading/writing binary
On Thu, May 7, 2009 at 12:24, John W. Krahn wrote: snip > Shouldn't that be either: > > sysopen ... > sysread ... > > Or: > > open ... > read ... > > ? snip If this were C, then yes, but this is Perl and sysread takes a filehandle. Just remember not to mix sysread and readline (or the operator form <>). -- Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Reading/writing binary
Raymond Wan wrote: Hi all, Hello, I would like to write binary values to disk (as well as read them) but don't know how to do it. In C-speak, something like this: unsigned int foo = 42; fwrite (&foo, sizeof (unsigned int), 1, stdout); I think the answer involves something with pack and unpack, but I'm completely lost as I have no experience with either. The closest I got was my $decimal_number = 42; my $binary_number = unpack("B32", pack("N", $decimal_number)); print "Decimal number " . $decimal_number . " is " . $binary_number . " in binary.\n\n"; In Perl numbers and strings are interchangable so: my $decimal_number = 42; and my $decimal_number = '42'; and my $decimal_number = "42"; all do the same thing. You could also write that as: my $decimal_number = 052; # octal representation of 42 or: my $decimal_number = 0x2A; # hexadecimal representation of 42 or: my $decimal_number = 0b101010; # binary representation of 42 See: perldoc perlnumber for more details. The C binary number 42 is represented in Perl as a string: my $decimal_number = "\x2A"; or: my $decimal_number = "\052"; That is equivalent in C to: unsigned char decimal_number = 42; Or another way to write that in Perl is: my $decimal_number = pack 'C', 42; Once you have created the appropriate strings using pack() then just print() them. John -- Those people who think they know everything are a great annoyance to those of us who do.-- Isaac Asimov -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Licensing code
Hi all, Although I am not a programmer, I do have a few dozen applications I've written. All programs concerned are in Perl. My 'programs' include numerous functions within dozens of library files which I've also personally written. Some of my applications utilize functions from other external (CPAN and one proprietary) Perl modules. I'm at the point where I want to ensure that all of my code can legally be put into the public domain, whether anyone wants to use it or not. I like the BSD license, and I am a BSD person, but I'm asking for feedback from the creme-de-la-creme with regards to making code public, so that I can put something in my code to ensure that someone else doesn't decide that they want to "claim" it. This is important to me. I'm willing to discuss on or off-list if further details are necessary. Steve -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
newbie needs help with first perl prog
Hi folks I am new to perl. this is my first real program in perl. The program is not doing what i intend it to do. The problem in nut shell is that: I expect to see the menu first and then input my choice. but the program asks for my choice first and then displays the menu Please have a look and see what am i doing wrong. Thanks a ton for your time guys. code -- #!/usr/bin/perl use strict; use warnings; sub dispBlanks { print "\n\n"; } sub clrScr { system("clear"); } sub dispMenu { clrScr; print "\n\n=== Student Manage Pro ===\n"; print "1. Display all students\n"; print "2. Display a particular student\n"; print "3. Add a new student\n"; print "4. Delete a student\n"; print "0. Exit\n"; print "Please choose one of the options\n"; } while (1) { dispMenu my $choice = ; chomp($choice); # to remove newline print "you chose $choice"; dispBlanks; } -- -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: newbie needs help with first perl prog
Prince Mavi 写道: > Hi folks > > I am new to perl. this is my first real program in perl. > The program is not doing what i intend it to do. > The problem in nut shell is that: > I expect to see the menu first and then input my choice. > but > the program asks for my choice first and then displays the menu > > Please have a look and see what am i doing wrong. > > Thanks a ton for your time guys. > > > code > -- > #!/usr/bin/perl > use strict; > use warnings; > > sub dispBlanks { > print "\n\n"; > } > > sub clrScr { > system("clear"); > } > > sub dispMenu { > clrScr; > print "\n\n=== Student Manage Pro ===\n"; > print "1. Display all students\n"; > print "2. Display a particular student\n"; > print "3. Add a new student\n"; > print "4. Delete a student\n"; > print "0. Exit\n"; > print "Please choose one of the options\n"; > } > > while (1) { > dispMenu > my $choice = ; > chomp($choice); # to remove newline > print "you chose $choice"; > dispBlanks; > } > -- > > > 1 #!/usr/bin/perl -w 2 use strict; 3 use warnings; 4 5 sub dispBlanks { 6 print "\n\n"; 7 } 8 9 sub clrScr { 10 system("clear"); 11 } 12 13 clrScr;/ #= 14 sub dispMenu { 15 print "\n\n=== Student Manage Pro ===\n"; 16 print "1. Display all students\n"; 17 print "2. Display a particular student\n"; 18 print "3. Add a new student\n"; 19 print "4. Delete a student\n"; 20 print "0. Exit\n"; 21 print "Please choose one of the options\n"; 22 } 23 24 my $choice = 1; 25 while ($choice) { 26 dispMenu; 27 chomp($choice = ); 28 print "you chose $choice"; 29 dispBlanks; 30 } -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: newbie needs help with first perl prog
Prince Mavi wrote: > Hi folks > > I am new to perl. this is my first real program in perl. > The program is not doing what i intend it to do. > The problem in nut shell is that: > I expect to see the menu first and then input my choice. > but > the program asks for my choice first and then displays the menu > > Please have a look and see what am i doing wrong. > > Thanks a ton for your time guys. Changes that I found to make it work: #!/usr/bin/perl use strict; use warnings; sub dispBlanks { print "\n\n"; } sub clrScr { system("clear"); } sub dispMenu { #clrScr; # <-- commented out print "\n\n=== Student Manage Pro ===\n"; print "1. Display all students\n"; print "2. Display a particular student\n"; print "3. Add a new student\n"; print "4. Delete a student\n"; print "0. Exit\n"; print "Please choose one of the options\n"; } while (1) { dispMenu(); # < missing semi-colon my $choice = ; chomp($choice); # to remove newline print "you chose $choice"; dispBlanks; } -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Licensing code
On Thu, May 7, 2009 at 22:04, Steve Bertrand wrote: snip > I like the BSD license, and I am a BSD person, but I'm asking for > feedback from the creme-de-la-creme with regards to making code public, > so that I can put something in my code to ensure that someone else > doesn't decide that they want to "claim" it. snip Given your desire to keep the code free (i.e. not allow people to create closed source forks), your best option is the GPLv2[1] or GPLv3[2] licenses. Perl itself and many of its modules are dual licensed under GPLv1[3] or later license and the Artistic License[4]. The GPL basically says that you must provide the source code in the preferred form to people you give or sell the binaries to (if they ask for it). The Artistic License is similar, but a bit more free (the GPL gets a bit hinky when you want to combine GPL and commercial code). Neither license prevents people from selling the software in question, but both require that source be available (or made available), so anyone charging an arm and a leg for it will rapidly find free versions being made available (see CentOS and RedHat for an example). Personally I like the Artistic License, especially over GPLv3, which is starting to get too political for me. 1. http://www.gnu.org/licenses/gpl-2.0.html 2. http://www.gnu.org/licenses/gpl-3.0.html 3. http://www.gnu.org/licenses/gpl-1.0.html 4. http://dev.perl.org/licenses/artistic.html -- Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Licensing code
Chas. Owens wrote: > On Thu, May 7, 2009 at 22:04, Steve Bertrand wrote: > snip >> I like the BSD license, and I am a BSD person, but I'm asking for >> feedback from the creme-de-la-creme with regards to making code public, >> so that I can put something in my code to ensure that someone else >> doesn't decide that they want to "claim" it. > snip > > Given your desire to keep the code free (i.e. not allow people to > create closed source forks), your best option is the GPLv2[1] or > GPLv3[2] licenses. Perl itself and many of its modules are dual > licensed under GPLv1[3] or later license and the Artistic License[4]. > > The GPL basically says that you must provide the source code in the > preferred form to people you give or sell the binaries to (if they ask > for it). > > The Artistic License is similar, but a bit more free (the GPL gets a > bit hinky when you want to combine GPL and commercial code). [snip] Thanks Chas, for the response. I'm a network engineer, naturally, I hate legal stuff. Given what you've said, I'll rephrase: I wish all of my code be free, ie: I don't care if a commercial entity uses it for their benefit or not, I don't care if derivatives are used in commercial products or not, all I really care about is that my current code is protected so that it can not be "claimed" as anyone elses, currently... if that makes sense. Am I allowed to put in any license I choose into files containing my own code, so long as I honour all licences within any modules which I may have included? If I do add a license into my files, does that take effect immediately? Steve -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/