Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Look at C++ as a scripting language and you possibly will not dislike it as much.


Agreed! I use C++ for small scripts/tools and love it. Granted, I first spent some time writing a library of convenience functions like starts_with(), ends_with(), contains(), get_file_contents(), read_csv_file(), etc. that so many languages have built into their standard libraries, but to be honest, even that part was a lot of fun!


CERN actually does that with CLING for interactive HEP data science.

https://root.cern/cling/


Thanks!

  $ brew install cling
  $ cling
   
  ****************** CLING ******************
  * Type C++ code and press enter to run it *
  *             Type .q to exit             *
  *******************************************
  [cling]$ #include <stdio.h>
  [cling]$ #include <sys/utsname.h>
  [cling]$ struct utsname u;
  [cling]$ uname(&u);
  [cling]$ printf("%s %s %s\n", u.sysname, u.release, u.machine);
  Darwin 20.5.0 arm64


You can even use it interactively like ipython and get rid of the printf, as it automatically evaluates expressions.

  [cling]$ int i=21
  (int) 21
  [cling]$ i*2
  (int) 42


Thanks! Is there a way to see strings nicely without printf?

  [cling]$ u.sysname
  (char [256]) "Darwin\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"


I imagine if you printed an actual string it would look as you expect, issue is that you're printing a char array and not a C++ std::string/_view


Indeed, this is the expected behavior with C arrays, regardless their type.

The solutions are either to use a C++ std::string as you mentioned, or a typedef struct with a length property.


struct utsname is what it is. For me the appeal for something like cling would be doing quick experiments without any boilerplate.


Sure, but I’d expect the interpreter to have some smarts to do the right thing within reason.


But a char[] could be binary data for all the tool knows, do you want it to do a buffer overflow and end up bricking your computer? std::string etc. exist exactly because char[] is unsafe for holding strings unless you carry around a size.


There is a size in the example I posted above.

  [cling]$ u.sysname
    (char [256])




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: