use strict; use warnings; use Geo::Gpx; use DateTime; # Open the GPX file open my $fh_in, '<', 'fells_loop.gpx'; # Parse GPX my $gpx = Geo::Gpx->new( input => $fh_in ); # Close the GPX file close $fh_in; # Open an output file open my $fh_out, '>', 'fells_loop.csv'; # Print the header line to the file print $fh_out "time,lat,lon,ele,name,sym,type,desc\n"; # The waypoints-method of the GEO::GPX-Object returns an array-ref # which we can iterate in a foreach loop foreach my $wp ( @{ $gpx->waypoints() } ) { # Some fields seem to be optional so they are missing in the hash. # We have to add an empty string by iterating over all the possible # hash keys to put '' in them. Map is like a foreach loop. map { $wp->{$_} ||= '' } qw( time lat lon ele name sym type desc ); # The time is a unix timestamp, which is hard to read. # We can make it an ISO8601 date with the DateTime module. # We only do it if there already is a time, though. if ($wp->{'time'}) { $wp->{'time'} = DateTime->from_epoch( epoch => $wp->{'time'} ) ->iso8601(); } # Join the fields with a comma and print them to the output file print $fh_out join(',', ( $wp->{'time'}, $wp->{'lat'}, $wp->{'lon'}, $wp->{'ele'}, $wp->{'name'}, $wp->{'sym'}, $wp->{'type'}, $wp->{'desc'}, )), "\n"; # Add a newline at the end } # Close the output file close $fh_out;